package com.example.management.system.service.impl



import com.example.management.common.domain.Tree
import com.example.management.common.utils.BuildTree
import com.example.management.system.dao.MenuDao
import com.example.management.system.dao.RoleMenuDao
import com.example.management.system.domain.MenuDO
import com.example.management.system.service.MenuService
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

import java.util.*

@Service
@Transactional(readOnly = true, rollbackFor = arrayOf(Exception::class))
class MenuServiceImpl : MenuService {
@Autowired
    internal var menuMapper: MenuDao? = null
    @Autowired
    internal var roleMenuMapper: RoleMenuDao? = null

    // 默认顶级菜单为０，根据数据库实际情况调整
    override val tree: Tree<MenuDO>
        get(){
            val trees = ArrayList<Tree<MenuDO>>()
            val menuDOs = menuMapper!!.list(HashMap<String, Any>(16))
            for (sysMenuDO in menuDOs) {
                val tree = Tree<MenuDO>()
                tree.id = (sysMenuDO.menuId.toString())
                tree.parentId=(sysMenuDO.parentId.toString())
                tree.text = (sysMenuDO.name)
                trees.add(tree)
            }
            // 默认顶级菜单为０，根据数据库实际情况调整
            return BuildTree.build(trees)!!
        }  //To change initializer of created properties use File | Settings | File Templates.

    /**
     * @param
     * @return 树形菜单
     */
    @Cacheable
    override fun getSysMenuTree(id: Long?): Tree<MenuDO> {
        val trees = ArrayList<Tree<MenuDO>>()
        val menuDOs = menuMapper!!.listMenuByUserId(id!!)
        for (sysMenuDO in menuDOs) {
            val tree = Tree<MenuDO>()
            tree.id = (sysMenuDO.menuId.toString())
            tree.parentId = (sysMenuDO.parentId.toString())
            tree.text = (sysMenuDO.name)
            val attributes = HashMap<String, Any>(16)
            attributes["url"] = sysMenuDO.url!!
            attributes["icon"] = sysMenuDO.icon!!
            tree.attributes = (attributes)
            trees.add(tree)
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        return BuildTree.build(trees)!!
    }

    override fun list(params: Map<String, Any>): List<MenuDO> {
        return menuMapper!!.list(params)
    }

    @Transactional(readOnly = false, rollbackFor = arrayOf(Exception::class))
    override fun remove(id: Long?): Int {
        return menuMapper!!.remove(id!!)
    }

    @Transactional(readOnly = false, rollbackFor = arrayOf(Exception::class))
    override fun save(menu: MenuDO): Int {
        return menuMapper!!.save(menu)
    }

    @Transactional(readOnly = false, rollbackFor = arrayOf(Exception::class))
    override fun update(menu: MenuDO): Int {
        return menuMapper!!.update(menu)
    }

    override operator fun get(id: Long?): MenuDO {
        return menuMapper!!.get(id!!)
    }

    override fun getTree(id: Long?): Tree<MenuDO> {
        // 根据roleId查询权限
        val menus = menuMapper!!.list(HashMap<String, Any>(16))
        val menuIds = roleMenuMapper!!.listMenuIdByRoleId(id)
        for (menu in menus) {
            if (menuIds.contains(menu.parentId)) {
                menuIds.remove(menu.parentId)
            }
        }
        val trees = ArrayList<Tree<MenuDO>>()
        val menuDOs = menuMapper!!.list(HashMap<String, Any>(16))
        for (sysMenuDO in menuDOs) {
            val tree = Tree<MenuDO>()
            tree.id = (sysMenuDO.menuId.toString())
            tree.parentId = (sysMenuDO.parentId.toString())
            tree.text = (sysMenuDO.name)
            val state = HashMap<String, Any>(16)
            val menuId = sysMenuDO.menuId
            if (menuIds.contains(menuId)) {
                state["selected"] = true
            } else {
                state["selected"] = false
            }
            tree.state = (state)
            trees.add(tree)
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        return BuildTree.build(trees)!!
    }

    override fun listPerms(userId: Long?): Set<String> {
        val perms = menuMapper!!.listUserPerms(userId!!)
        val permsSet = HashSet<String>()
        for (perm in perms) {
            if (StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(*perm.trim({ it <= ' ' }).split(",".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()))
            }
        }
        return permsSet
    }

    override fun listMenuTree(id: Long?): List<Tree<MenuDO>> {
        val trees = ArrayList<Tree<MenuDO>>()
        val menuDOs = menuMapper!!.listMenuByUserId(id!!)
        for (sysMenuDO in menuDOs) {
            val tree = Tree<MenuDO>()
            tree.id = (sysMenuDO.menuId.toString())
            tree.parentId = (sysMenuDO.parentId.toString())
            tree.text = (sysMenuDO.name)
            val attributes = HashMap<String, Any>(16)
            attributes["url"] = if (sysMenuDO.url == null ) "" else sysMenuDO.url!!
            attributes["icon"] = if (sysMenuDO.icon == null) "" else sysMenuDO.icon!!
            tree.attributes = (attributes)
            trees.add(tree)
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        return BuildTree.buildList(trees, "0")!!
    }

}
